﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;

namespace Site
{
    public partial class Menu_Technology : UserControl
    {
        private Storyboard storyFadeIn1;
        private Storyboard storyFadeOut1;
        private Storyboard storyFadeIn2;
        private Storyboard storyFadeOut2;
        private Storyboard storyFadeIn3;
        private Storyboard storyFadeOut3;
        private Storyboard storyFadeIn4;
        private Storyboard storyFadeOut4;

        /// <summary>
        /// 图标淡入效果
        /// </summary>
        /// <param name="fadein"></param>
        /// <param name="fadeout"></param>
        /// <param name="img"></param>
        private void FadeIn(ref Storyboard fadein, ref Storyboard fadeout, Image img)
        {
            if (null != fadeout && fadeout.GetCurrentState() != ClockState.Stopped)
            {
                fadeout.Pause();
            }

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = img.Opacity;
            animation.To = 1;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, img);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Image.OpacityProperty));

            fadein = new Storyboard();
            fadein.Children.Add(animation);
            fadein.Begin();
        }

        /// <summary>
        /// 图片淡出效果
        /// </summary>
        /// <param name="fadein"></param>
        /// <param name="fadeout"></param>
        /// <param name="img"></param>
        private void FadeOut(ref Storyboard fadein, ref Storyboard fadeout, Image img)
        {
            if (null != fadein && fadein.GetCurrentState() != ClockState.Stopped)
            {
                fadein.Pause();
            }
            fadein = null;

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = img.Opacity;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, img);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Image.OpacityProperty));


            fadeout = new Storyboard();
            fadeout.Children.Add(animation);
            fadeout.Begin();
        }

        /// <summary>
        /// 外发光效果
        /// </summary>
        private DropShadowEffect EffectShadow()
        {
            System.Windows.Media.Effects.DropShadowEffect ds = new System.Windows.Media.Effects.DropShadowEffect();
            ds.Color = Colors.White;
            ds.BlurRadius = 100;
            ds.ShadowDepth = 0;
            return ds;
        }

        public Menu_Technology()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Menu_Technology_Loaded);
        }

        void Menu_Technology_Loaded(object sender, RoutedEventArgs e)
        {
            //图标外发光效果
            DropShadowEffect effect = EffectShadow();
            imgTech1_1.Effect = effect;
            imgTech2_1.Effect = effect;
            imgTech3_1.Effect = effect;
            imgTech4_1.Effect = effect;
        }

        private void imgTech1_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn1, ref storyFadeOut1, imgTech1_1);
        }

        private void imgTech1_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn1, ref storyFadeOut1, imgTech1_1);
        }

        private void imgTech2_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn2, ref storyFadeOut2, imgTech2_1);
        }

        private void imgTech2_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn2, ref storyFadeOut2, imgTech2_1);
        }

        private void imgTech3_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn3, ref storyFadeOut3, imgTech3_1);
        }

        private void imgTech3_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn3, ref storyFadeOut3, imgTech3_1);
        }

        private void imgTech4_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn4, ref storyFadeOut4, imgTech4_1);
        }

        private void imgTech4_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn4, ref storyFadeOut4, imgTech4_1);
        }

        private void imgTech1_1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show("Tech1");
            App app = (App)Application.Current;
            Tech_Detail t = new Tech_Detail();
            t.DefaultTech = 0;
            app.RedirectTo(t);
        }

        private void imgTech2_1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show("Tech2");
            App app = (App)Application.Current;
            Tech_Detail t = new Tech_Detail();
            t.DefaultTech = 1;
            app.RedirectTo(t);
        }

        private void imgTech3_1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show("Tech3");
            App app = (App)Application.Current;
            Tech_Detail t = new Tech_Detail();
            t.DefaultTech = 2;
            app.RedirectTo(t);
        }

        private void imgTech4_1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App app = (App)Application.Current;
            Tech_Detail t = new Tech_Detail();
            t.DefaultTech = 3;
            app.RedirectTo(t);
        }
    }
}
